package com.jt.www.util;

import org.apache.commons.lang.StringUtils;

public class MyStringUtils {

    public static final char UNDERLINE = '_';

    /**
     * 文件下载url
     */
    public static final String FILE_URL = "/res-svc/res/v3/download?applicationName=%s&businessTypeCode=%s&businessDataTag=%s&free=%s&fileId=%s";

    /**
     * 下划线格式字符串转换为驼峰格式字符串
     *
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String subPre(String string, int toIndex) {
        return sub(string, 0, toIndex);
    }

    public static String sub(String string, int fromIndex, int toIndex) {
        int len = string.length();
        if (fromIndex < 0) {
            fromIndex += len;
            if (fromIndex < 0) {
                fromIndex = 0;
            }
        } else if (fromIndex > len) {
            fromIndex = len;
        }

        if (toIndex < 0) {
            toIndex += len;
            if (toIndex < 0) {
                toIndex = len;
            }
        } else if (toIndex > len) {
            toIndex = len;
        }

        if (toIndex < fromIndex) {
            int tmp = fromIndex;
            fromIndex = toIndex;
            toIndex = tmp;
        }

        return fromIndex == toIndex ? "" : string.substring(fromIndex, toIndex);
    }

    public static boolean endWithIgnoreCase(String str, String suffix) {
        return endWith(str, suffix, true);
    }

    public static boolean endWith(String str, String suffix, boolean isIgnoreCase) {
        return isIgnoreCase ? str.toLowerCase().endsWith(suffix.toLowerCase()) : str.endsWith(suffix);
    }

    /**
     * 隐藏字符
     *
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static String hiddenCharacters(String str, int start, int end) {
        if (StringUtils.isBlank(str) || str.length() <= 1 || end < 0) {
            return str;
        }
        if (str.length() == 2 && end > 0) {
            return str.substring(0, 1).concat("*");
        }
        if (end >= str.length()) {
            end = str.length();
        }
        if (start >= end) {
            return str;
        }
        return str.substring(0, start).concat("****").concat(str.substring(end));
    }


    /**
     * 得到文件下载地址
     *
     * @return
     */
    public static String createFileUrl(String applicationName, String businessTypeCode, String businessDataTag, String free, String fileId) {
        // 样例 applicationName=ins-svc&businessTypeCode=ins_app&businessDataTag=T2005120948016394680&free=0&fileId=2124851953489150213
        return String.format(FILE_URL, applicationName, businessTypeCode, businessDataTag, free, fileId);

    }

    /**
     * 对字符串做掩码
     **/
    public static String hiddenCharacters(String str) {
        if (str == null || str.length() == 0) return "";
        String v = str.substring(0, 4);
        String end = str.substring(str.length() - 4);
        return v + StringUtils.repeat("*", str.length() - 8) + end;
    }
}
